package com.qf.flash.ui.circle;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qf.flash.R;
import com.qf.flash.app.MyApp;
import com.qf.flash.model.CapacityBean;
import com.qf.flash.model.CapacityRankThreeBean;
import com.qf.flash.model.ListBottomBean;
import com.qf.flash.model.ListTopBean;
import com.qf.flash.mvp.contract.CapacityContract;
import com.qf.flash.mvp.presenter.CapacityPresenter;
import com.qf.flash.ui.circle.adapter.ListBottomItem;
import com.qf.flash.ui.circle.adapter.ListTopItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.LazyBaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ListFragment extends LazyBaseFragment implements CapacityContract.View {

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    Unbinder unbinder;
    private RVAdapter<Object> mAdapter;
    private CapacityPresenter capacityPresenter;
    private String type;
    @Override
    protected int setContentView() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initInject() {
        super.initInject();
        capacityPresenter = new CapacityPresenter();
        initPresenters(capacityPresenter);
    }

    @Override
    protected void initView() {
        mAdapter = new RVAdapter<>(new ListTopItem(getActivity()), new ListBottomItem());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapter);

    }

    public static ListFragment newInstance(String type) {
        ListFragment myFragment = new ListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Override
    protected void lazyLoad() {
        type = getArguments().getString("type");
        capacityPresenter.getCapacityData(type,1,30);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setCapacityData(List<CapacityBean> capacityBeans, int totalPage) {
        List<Object> datas = mAdapter.getDatas();
        List<CapacityBean>  topBean= new ArrayList<>();
        for (int i = 0; i < capacityBeans.size(); i++) {
            if(i<3){
                topBean.add(capacityBeans.get(i));
            }
        }
        CapacityRankThreeBean capacityRankThreeBean = new CapacityRankThreeBean();
        capacityRankThreeBean.capacityBeans = topBean;
        // 添加前三
        datas.add(capacityRankThreeBean);
        // 添加下面的列表
        if(capacityBeans.size()>3){
            for (int i = 0; i < 3; i++) {
                capacityBeans.remove(0);
            }
        }
        datas.addAll(capacityBeans);
        mAdapter.notifyDataSetChanged();

    }
}
